﻿


using System;
using System.Linq;
using Demo.Domain;
using Demo.Repository;

namespace Demo.Repository
{
    public class RoleRepository : IRoleRepository
    {

        DemoContext db = new DemoContext();

        public Role GetByName(string roleName)
        {
            return db.Role.Where(r => r.Name == roleName).FirstOrDefault();
        }

        public bool Exists(string roleName)
        {
            return db.Role.Where(r => r.Name == roleName).Count() > 0;
        }

        public Role CreateRole(string roleName, int sort)
        {
            var role = new Role {  Name = roleName, Sort = sort};
            return db.Role.Add(role);
        }

        public bool DeleteRole(int roleID)
        {
            var role = db.Role.Where(r=>r.RoleID == roleID).FirstOrDefault();
            db.Role.Remove(role);
            return true;
        }

        public void ChangeRole(Role role)
        {
            db.Entry<Role>(role).State = System.Data.EntityState.Modified;
            db.SaveChanges();
        }
    }
}
